import settings from '../settings'
import { getAuthen } from '@/utils/getAuthen'
import moment from 'moment'
import storage from '@/utils/storageTool'
import {getEnterpriseId} from "@/utils/getEnterpriseId"


const noncestr = Math.random().toString(36).substring(2);
const timestamp = +moment.utc();


// 带Origin后缀的通过getter获取
export const state = () => ({
  openIdOrigin: storage.getItem('openId')||"",
  accessTokenOrigin: storage.getItem('accessToken')||"",
  unionIdOrigin: storage.getItem('unionId')||"",
  userInfo:JSON.parse((!!storage.getItem('userInfo') ?  storage.getItem('userInfo') :'{}' ))||{},
  enterpriseIdOrigin: getEnterpriseId(),
  appIdOrigin: storage.getItem('appId')||"",
  phoneOrigin:storage.getItem('phone')||"",
  noncestr,
  timestamp,
  signature:"",
})

export const getters = {
  openId: (state) => {
    if (!state.openIdOrigin) {
      // getAuthen()       // 手机号做标识 openid 不再作为拉起认证的入口
    }
    return state.openIdOrigin
  },
  accessToken: (state) => {
    if (!state.accessTokenOrigin) {
      console.log('------accessTokenOrigin-----');
      getAuthen()
    }
    return state.accessToken
  },
  enterpriseId: (state) => {
    if (!state.enterpriseIdOrigin) {

      // getAuthen()
      alert('请通过站点网址访问~' + state.enterpriseIdOrigin)
    }
    return state.enterpriseIdOrigin
  },
  appId: (state) => {
    if (!state.appIdOrigin) {
      console.log('------appIdOrigin-----');
      getAuthen()
    }
    return state.appIdOrigin
  },
  unionId: (state) => {
    if (!state.unionIdOrigin) {
      // getAuthen()
    }
    return state.unionIdOrigin
  },
  phone: (state) => {
    if (!state.phoneOrigin) {
      console.log('------phoneOrigin-----');
      getAuthen()
    }
    return state.phoneOrigin
  },
  userInfo: (state) => state.userInfo,
}

export const mutations = {
  setOpenId(state, openId) {
    if(!openId){
      openId = ''
    }
    storage.setItem('openId',openId)
    state.openIdOrigin = openId
  },
  setPhone: (state, phone) => {
    if(!phone){
      phone = '' 
    }
    storage.setItem('phone',phone)
    state.phoneOrigin = phone
  },
  setAccessToken(state, accessToken) {
    storage.setItem('accessToken',accessToken)
    state.accessTokenOrigin = accessToken
  },
  setUnionId(state, unionId) {
    if(!unionId){
      unionId = '' 
    }
    storage.setItem('unionId',unionId)
    state.unionIdOrigin = unionId
  },
  setUserInfo(state, userInfo) {
    storage.setItem('userInfo',JSON.stringify(userInfo))
    state.userInfo = userInfo
  },
  setSignature(state, signature) {
    state.signature = signature
  },
  setEnterpriseId(state,enterpriseId){
    state.enterpriseIdOrigin = enterpriseId
  },
  setAppId(state, appId) {
    storage.setItem('appId',appId)
    state.appIdOrigin = appId
  }
}

export const actions = {
  // async login({ commit, state, getters }, query) {
    
  //   const gzhAuthenRes = await this.$axios.$post("/selectCardMobilePhone", query)
  //   if (gzhAuthenRes.retCode !== 0) { return Promise.reject("拉取手机号失败") }
  //   console.log('拉取成功', gzhAuthenRes)
  //   const openId = query.openid;
  //   const phone = gzhAuthenRes.data.phone;
  //   const unionId = gzhAuthenRes.data.unionid;
  //   commit("setOpenId", openId)
  //   commit ("setPhone", phone)
  //   commit ("setUnionId", unionId)

  //   // commit("setUnionId", unionId)
  //   // const getUserInfoRes = await this.$axios.$get("/getGzhUserInfo", {
  //   //   params: {
  //   //     openId: openId,
  //   //     enterpriseId: getters.enterpriseId
  //   //   }
  //   // })
  //   // if (getUserInfoRes.retCode !== 0) { return Promise.reject("拉取用户信息失败") }
  //   // commit("setUserInfo", getUserInfoRes.data)
  //   // const saveUserInfoRes = await this.$axios.$post("/addAppUserInfo", {
  //   //   appType: "1",
  //   //   openId: openId,
  //   //   unionId: unionId,
  //   //   enterpriseId: getters.enterpriseId, //油站Id
  //   //   blogNickName: getUserInfoRes.data.nickname,
  //   //   sexFlag: getUserInfoRes.data.sex,
  //   //   blogProfilePhoto: getUserInfoRes.data.headimgurl
  //   // })
  //   // if (saveUserInfoRes.retCode !== 0) { return Promise.reject("存取用户信息失败") }
  //   return Promise.resolve("用户登录成功")
  // },  
  // async loginGetOpenId({ commit, state, getters }, code) {
  //   const gzhAuthenRes = await this.$axios.$get("/getGzhAuthorizeInfo", {
  //     params: {
  //       code,
  //       enterpriseId: getters.enterpriseId,
  //     },
  //   })
  //   if (gzhAuthenRes.retCode !== 0) { return Promise.reject("拉取openId失败") }
  //   console.log('拉取成功', gzhAuthenRes)
  //   const openId = gzhAuthenRes.data.openid;
  //   console.log('openId', openId)
  //   // const accessToken = gzhAuthenRes.data.access_token;
  //   // const unionId = gzhAuthenRes.data.unionid;
  //   commit("setOpenId", openId)
  //   // commit("setAccessToken", accessToken)
  //   // commit("setUnionId", unionId)
  //   // const getUserInfoRes = await this.$axios.$get("/getGzhUserInfo", {
  //   //   params: {
  //   //     openId: openId,
  //   //     enterpriseId: getters.enterpriseId
  //   //   }
  //   // })
  //   // if (getUserInfoRes.retCode !== 0) { return Promise.reject("拉取用户信息失败") }
  //   // commit("setUserInfo", getUserInfoRes.data)
  //   // const saveUserInfoRes = await this.$axios.$post("/addAppUserInfo", {
  //   //   appType: "1",
  //   //   openId: openId,
  //   //   unionId: unionId,
  //   //   enterpriseId: getters.enterpriseId, //油站Id
  //   //   blogNickName: getUserInfoRes.data.nickname,
  //   //   sexFlag: getUserInfoRes.data.sex,
  //   //   blogProfilePhoto: getUserInfoRes.data.headimgurl
  //   // })
  //   // if (saveUserInfoRes.retCode !== 0) { return Promise.reject("存取用户信息失败") }
  //   return Promise.resolve("用户获取openid成功")
  // },
  async getSdkConfig({commit,state, getters},config){
    const sdkSignRes = await this.$axios.$get("/getSignature",{
      params:{
        noncestr:state.noncestr,
        timestamp:state.timestamp,
        url:window.location.href.replace(/#(\w|\W){0,}/g, ''),      
        enterpriseId:getters.enterpriseId
        // enterpriseId:7

      }
    })
    if(sdkSignRes.retCode !== 0){Promise.reject("获取Sdk签名错误")}

    commit("setSignature", sdkSignRes.data)

    return Promise.resolve({
      debug: settings.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: getters.appId, // 必填，公众号的唯一标识
      // appId:'wxc8189d3b3a7283e0',
      timestamp: state.timestamp, // 必填，生成签名的时间戳
      nonceStr: state.noncestr, // 必填，生成签名的随机串
      signature:sdkSignRes.data,
      jsApiList:config.jsApiList, // 必填，需要使用的JS接口列表
      openTagList:config.openTagList // 跳转小程序时必填
    })
  },
  async getAppId({commit,state, getters},enterpriseId){
    const appIdRes = await this.$axios.$get("/getStationGzhAppId",{
      params:{
        enterpriseId:enterpriseId
      }
    })
    console.log('appIdRes',appIdRes)
    console.log('appIdRes', appIdRes.data.appId)
    if(appIdRes.retCode !== 0){return Promise.reject("获取appid失败")}
    commit("setEnterpriseId",enterpriseId)
    commit("setAppId", appIdRes.data.appId)
    return Promise.resolve({
      enterpriseId,
      appId:appIdRes.data.appId
    })
  }
}
